package ro.gateway.aida.admin;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.utils.HttpUtils;
import ro.xblue.translator.LanguageBean;
import ro.xblue.translator.TranslatorUtils;

/**
 * Created by IntelliJ IDEA.
 * User: smike
 * Date: Apr 19, 2004
 * Time: 9:10:15 PM
 * To change this template use File | Settings | File Templates.
 */
public class PropertyListItem$EditAction extends Action {
	public ActionForward execute(ActionMapping mapping,
						    ActionForm form_,
						    HttpServletRequest request,
						    HttpServletResponse response)
		 throws Exception {

		PersistenceToken token = PersistenceManager.
			 tokenLookup(this.getServlet().getServletContext());
		String ctx = request.getContextPath();

		PropertyListItem$EditForm form = (PropertyListItem$EditForm) form_;
		form.event = HttpUtils.getValidTrimedString(request, "event", "start");
		LanguageBean lang_bean = TranslatorUtils.getCurrentLanguage(request, this.getServlet().getServletContext());
		if (form.item != null) {
			form.item.setViewLang(lang_bean.language);
		}

		if ("start".equals(form.event)) {
			form.action = HttpUtils.getValidTrimedString(request, "action", "create");

			if ("create".equals(form.action)) {
				String s_list = HttpUtils.getValidTrimedString(request, "list", null);
				form.list = PropertyListDB.getManager(token).getList(s_list);
				if (form.list == null) {
					response.sendError(HttpServletResponse.SC_NOT_FOUND);
					return null;
				}

				form.item = new PropertyListItem();
				form.item.setList_id(form.list.getId());
			} else if ("edit".equals(form.action)) {
				long id = HttpUtils.getLong(request, "id", -1);
				form.item = PropertyListDB.getManager(token).getItemById(id);
				if (form.item == null) {
					response.sendError(HttpServletResponse.SC_NOT_FOUND);
					return null;
				}
				form.list = PropertyListDB.getManager(token).getListById(form.item.getList_id(), false);
			}

			response.sendRedirect(ctx + "/admin/editproplistitem.do?event=edit");
			return null;
		} else if ("update".equals(form.event)) {
			form.item.setViewValue(HttpUtils.getValidTrimedString(request, "viewValue", null));
			PropertyListDB.getManager(token).insertOrUpdateItem(form.item);

			response.sendRedirect(ctx + "/admin/editproplistitem.do?event=edit");
			return null;
		} else if ("edit".equals(form.event)) {
			return mapping.findForward("jsp_page");
		} else if ("delsel".equals(form.event)) {
			String s_list = HttpUtils.getValidTrimedString(request, "list", null);
			form.list = PropertyListDB.getManager(token).getList(s_list);
			if (form.list == null) {
				response.sendError(HttpServletResponse.SC_NOT_FOUND);
				return null;
			}

			int some_idx = HttpUtils.getInt(request, "idxs", -1);
			if (some_idx == -1) form.idxs = null;

			int[] idxs = form.idxs;
			ArrayList items = form.list.getItems();
			if (idxs != null && items != null && items.size() > 0)
				for (int i = 0; i < idxs.length; i++) {
					int idx = idxs[i];
					if (idx >= 0 && idx <= items.size()) {
						PropertyListItem item = (PropertyListItem) items.get(idx);
						PropertyListDB.getManager(token).deleteItem(item.getId());
					}
				}

			response.sendRedirect(ctx + "/admin/viewproplist.do?event=start&name=" + form.list.list_name);
			return null;
		}

		return mapping.findForward("jsp_page");
	}
}
